package com.lijin.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lijin.Exception.TimeParseException;
import com.lijin.converter.LotteryInformationConverter;
import com.lijin.dao.LotteryDao;
import com.lijin.dao.LotteryInformationDao;
import com.lijin.domain.dto.LotteryInformationDto;
import com.lijin.domain.entity.LotteryInformation;
import com.lijin.domain.enums.LotteryCodeEnums;
import com.lijin.domain.json.LotteryData;
import com.lijin.domain.query.LotteryInformationQuery;
import com.lijin.domain.query.base.Page;
import com.lijin.service.LotteryInformationService;
import com.lijin.util.DataUtil;
import com.lijin.util.JsoupUtil;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * Created by gold-abbott on 2017/9/16.
 */
@Service
@Slf4j
public class LotteryInformationServiceImpl implements LotteryInformationService {

    @Autowired
    private LotteryInformationDao lotteryInformationDao;

    @Autowired
    private LotteryInformationConverter lotteryInformationConverter;

    @Autowired
    private LotteryDao lotteryDao;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    Environment env;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public Page<LotteryInformationDto> findInformationByCode(LotteryInformationQuery lotteryInformationQuery) {
        int count = lotteryInformationDao.findCountByQuery(lotteryInformationQuery);
        List<LotteryInformationDto> lotteryInformationDtos = null;
        if (count > 0) {
            List<LotteryInformation> lotteryInformations = lotteryInformationDao.findListByQuery(lotteryInformationQuery);
            lotteryInformationDtos = lotteryInformationConverter.convertToDto(lotteryInformations);
        }
        if (CollectionUtils.isEmpty(lotteryInformationDtos)) {
            return new Page<>(count, lotteryInformationDtos);
        }
        return new Page<>(count, lotteryInformationDtos);
    }

    @Override
    public List<LotteryInformationDto> findAllFirst() {
        List<LotteryInformation> lotteryInformations = lotteryInformationDao.findNewInfo();
        List<LotteryInformationDto> lotteryInformationDtos=lotteryInformationConverter.convertToDto(lotteryInformations);
        return lotteryInformationDtos;
    }

    @Override
    public LotteryInformationDto findInformationByCodeAndExpect(String code, String expect) {
        LotteryInformation lotteryInformation=lotteryInformationDao.findInformationByCodeAndExpect(code,expect);
        LotteryInformationDto lotteryInformationDto=lotteryInformationConverter.convertToDto(lotteryInformation);
        return lotteryInformationDto;
    }


    @Override
    public void addyiDingLotteryInformation(String code, int a, String url) {
        //String url = env.getProperty("url.ydniu." + code);

        LotteryData lotteryData = null;
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "ydn网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    lotteryData = JsoupUtil.YiDingNiu(url, code, a);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "ydn网的" + code + " 时间超", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "ydn网的" + code + " 正在开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "ydn网的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            Assert.isTrue(!(lotteryData == null), "没有抓取到信息");
            // 1.2 抓取成功 next
            // 2.将版本升级

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("ydniu"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("ydniu");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "ydn网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "ydn网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void addCaiPiaoYunLotteryInformation(String code, String url) {
        //String url = env.getProperty("url.cpy." + code);

        LotteryData lotteryData = null;
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "cpy网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    lotteryData = JsoupUtil.CaiPiaoYun(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "cpy网的" + code + " 时间超", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "cpy网的" + code + " 正在开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "cpy网的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            Assert.isTrue(!(lotteryData == null), "没有抓取到信息");
            // 1.2 抓取成功 next
            // 2.将版本升级

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("cpy"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("cpy");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "cpy网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "cpy网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    @Transactional
    public void addJSYL(String code, String url) {

        //String url = env.getProperty("url.jinsha." + code);
        String codeurl = env.getProperty("url.jinshaonly." + code);
        LotteryData lotteryData = null;
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "澳門金沙娱乐的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryData = jsoup.jinShaYuLe(url, codeurl, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "澳門金沙娱乐的" + code + " 时间超时", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "澳門金沙娱乐的" + code + "类型转化异常", e.getMessage());
                } catch (TimeParseException e) {
                    log.error("访问 " + "澳門金沙娱乐的" + code + "还没开奖", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "澳門金沙娱乐的" + code + "还没开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "澳門金沙娱乐的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("jinsha"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("jinsha");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "澳門金沙娱乐" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "澳門金沙娱乐" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void addHuangCai(String code, String url) {
        //String url = env.getProperty("url.hgc." + code);
        List<LotteryData> lotteryDatas = new ArrayList<>();
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "皇冠官网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.huangCai(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "皇冠官网的" + code + " 时间超时", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "皇冠官网的" + code + "解析数据异常", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "皇冠官网的" + code + "类型转化异常", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "皇冠官网的" + code + "其他异常", e.getMessage());
                }
                if (lotteryDatas == null || lotteryDatas.size() == 0) {
                    state++;
                } else {
                    break;
                }
            }
            for (LotteryData lotteryData : lotteryDatas) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                LotteryInformation lotteryInformation = null;
                lotteryInformation = lotteryInformationDao
                        .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
                if (lotteryInformation != null) {
                    if (lotteryInformation.getVersion() > 0) {
                        return;
                    }
                    if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("hgc"
                            .equals(lotteryInformation.getSource()))) {
                        lotteryInformationDao.addVersion(lotteryInformation.getId());
                    }
                    return;
                }
                lotteryInformation = new LotteryInformation();
                lotteryInformation.setLotteryCode(code);
                lotteryInformation.setLotteryExpect(lotteryData.getExpect());
                if (code.equals("HF_GDD11") || code.equals("HF_AHD11")) {
                    lotteryInformation.setOpenCode(DataUtil.chuliCode(lotteryData.getOpencode()));
                } else {

                    lotteryInformation.setOpenCode(lotteryData.getOpencode());
                }
                lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
                lotteryInformation.setState("NORMAL");
                if (judge(code)) {
                    lotteryInformation.setVersion(2);
                } else {
                    lotteryInformation.setVersion(0);
                }
                lotteryInformation.setSource("hgc");
                lotteryInformationDao.save(lotteryInformation);
                log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                        + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                        + " ,[开奖源]:" + lotteryInformation.getSource());
            }
            //log.info("结束抓取" + "皇冠官网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "皇冠官网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    @Transactional
    public void addWeiNiSiRen(String code, String url) {

        List<LotteryData> lotteryDatas = new ArrayList<>();
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "威尼斯人官网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.wnisiren(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "威尼斯人官网的" + code + " 时间超时", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "威尼斯人官网的" + code + "解析数据异常", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "威尼斯人官网的" + code + "类型转化异常", e.getMessage());
                }
                if (lotteryDatas == null || lotteryDatas.size() == 0) {
                    state++;
                } else {
                    break;
                }
            }
            //for (LotteryData lotteryData : lotteryDatas) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryDatas.get(0).getExpect(),
                            lotteryDatas.get(0).getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryDatas.get(0).getOpencode())
                        && !("weinisiren".equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryDatas.get(0).getExpect());
            lotteryInformation.setOpenCode(lotteryDatas.get(0).getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryDatas.get(0).getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("weinisiren");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //}
            //log.info("结束抓取" + "威尼斯人官网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "威尼斯人官网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    /***
     * caijingwang
     */
    @Override
    public void addCaiJingWangLotteryInformation() {
        List<LotteryData> lotteryDatas = new ArrayList<>();
        String url = env.getProperty("url.caijing");
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "财经网的" + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.caijingwang(url);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "财经网的数据时间超时", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "财经网的数据解析数据异常", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "财经网的数据类型转化异常", e.getMessage());
                }
                if (lotteryDatas == null || lotteryDatas.size() == 0) {
                    state++;
                } else {
                    break;
                }
            }
            for (LotteryData lotteryData : lotteryDatas) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                LotteryInformation lotteryInformation = null;
                Thread.sleep(500);
                lotteryInformation = lotteryInformationDao
                        .findByCodeAndExpect(lotteryData.getCode(), lotteryData.getExpect(),
                                lotteryData.getOpencode());
                if (lotteryInformation != null) {
                    if (lotteryInformation.getVersion() > 0) {
                        continue;
                    }
                    if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("cj"
                            .equals(lotteryInformation.getSource()))) {
                        lotteryInformationDao.addVersion(lotteryInformation.getId());
                    }
                    continue;
                }
                lotteryInformation = new LotteryInformation();
                lotteryInformation.setLotteryCode(lotteryData.getCode());
                lotteryInformation.setLotteryExpect(lotteryData.getExpect());
                lotteryInformation.setOpenCode(lotteryData.getOpencode());
                lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
                lotteryInformation.setState("NORMAL");
                if (judge(lotteryData.getCode())) {
                    lotteryInformation.setVersion(2);
                } else {
                    lotteryInformation.setVersion(0);
                }

                lotteryInformation.setSource("cj");
                lotteryInformationDao.save(lotteryInformation);
                log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                        + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                        + " ,[开奖源]:" + lotteryInformation.getSource());

                //log.info("结束抓取" + "财经官网的" + lotteryData.getCode() + "开奖信息");
            }
        } catch (Exception e) {
            log.error("添加" + "财经官网的开奖信息失败", e.getMessage());
        }

    }

    @Override
    public void addCaiPiao55LotteryInformation() {
        List<LotteryData> lotteryDatas = new ArrayList<>();
        String url = env.getProperty("url.cp55");
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "彩票55网的" + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.caipiao55(url);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "彩票55网的数据时间超时", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "彩票55网的数据解析数据异常", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "彩票55网的数据类型转化异常", e.getMessage());
                }
                if (lotteryDatas == null || lotteryDatas.size() == 0) {
                    state++;
                } else {
                    break;
                }
            }
            for (LotteryData lotteryData : lotteryDatas) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                LotteryInformation lotteryInformation = null;
                Thread.sleep(300);
                lotteryInformation = lotteryInformationDao
                        .findByCodeAndExpect(lotteryData.getCode(), lotteryData.getExpect(),
                                lotteryData.getOpencode());
                if (lotteryInformation != null) {
                    if (lotteryInformation.getVersion() > 0) {
                        continue;
                    }
                    if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("cp55"
                            .equals(lotteryInformation.getSource()))) {
                        lotteryInformationDao.addVersion(lotteryInformation.getId());
                    }
                    continue;
                }
                lotteryInformation = new LotteryInformation();
                lotteryInformation.setLotteryCode(lotteryData.getCode());
                lotteryInformation.setLotteryExpect(lotteryData.getExpect());
                lotteryInformation.setOpenCode(lotteryData.getOpencode());
                lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
                lotteryInformation.setState("NORMAL");
                if (judge(lotteryData.getCode())) {
                    lotteryInformation.setVersion(2);
                } else {
                    lotteryInformation.setVersion(0);
                }
                lotteryInformation.setSource("cp55");
                lotteryInformationDao.save(lotteryInformation);
                log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                        + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                        + " ,[开奖源]:" + lotteryInformation.getSource());

                //log.info("结束抓取" + "彩票55官网的" + lotteryData.getCode() + "开奖信息");
            }
        } catch (Exception e) {
            log.error("添加" + "彩票55官网的开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void add5gcpLotteryInformation(String code, String url) {
        LotteryData lotteryData = null;
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "5gcp网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryData = jsoup.c5gcpLotty(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "5gcp网的" + code + " 时间超", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "5gcp网的" + code + " 正在开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "5gcp网的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            Assert.isTrue(!(lotteryData == null), "没有抓取到信息");
            // 1.2 抓取成功 next
            // 2.将版本升级

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("5gcp"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("5gcp");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "5gcp网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "5gcp网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void addCaiPiaoqqLotteryInformation() {
        List<LotteryData> lotteryDatas = new ArrayList<>();
        String url = env.getProperty("url.qqcp");
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "彩票qqcp网的" + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.caipiaoqq(url);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "彩票qqcp网的数据时间超时", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "彩票qqcp网的数据解析数据异常", e.getMessage());
                } catch (IOException e) {
                    log.error("访问 " + "彩票qqcp网的数据类型转化异常", e.getMessage());
                }
                if (lotteryDatas == null || lotteryDatas.size() == 0) {
                    state++;
                } else {
                    break;
                }
            }
            for (LotteryData lotteryData : lotteryDatas) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                LotteryInformation lotteryInformation = null;

                Thread.sleep(200);
                lotteryInformation = lotteryInformationDao
                        .findByCodeAndExpect(lotteryData.getCode(), lotteryData.getExpect(),
                                lotteryData.getOpencode());
                if (lotteryInformation != null) {
                    if (lotteryInformation.getVersion() > 0) {
                        continue;
                    }
                    if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("qqcp"
                            .equals(lotteryInformation.getSource()))) {
                        lotteryInformationDao.addVersion(lotteryInformation.getId());
                    }
                    continue;
                }
                lotteryInformation = new LotteryInformation();
                lotteryInformation.setLotteryCode(lotteryData.getCode());
                lotteryInformation.setLotteryExpect(lotteryData.getExpect());
                lotteryInformation.setOpenCode(lotteryData.getOpencode());
                lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
                lotteryInformation.setState("NORMAL");
                if (judge(lotteryData.getCode())) {
                    lotteryInformation.setVersion(2);
                } else {
                    lotteryInformation.setVersion(0);
                }
                lotteryInformation.setSource("qqcp");
                lotteryInformationDao.save(lotteryInformation);

                log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                        + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                        + " ,[开奖源]:" + lotteryInformation.getSource());
                //log.info("结束抓取" + "彩票qqcp官网的" + lotteryData.getCode() + "开奖信息");
            }
        } catch (Exception e) {
            log.error("添加" + "彩票qqcp官网的开奖信息失败", e.getMessage());
        }
    }


    @Override
    public void addFengHuangLotteryInformation(String code, String url) {
        LotteryData lotteryData = null;
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "http://5559965.com网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryData = jsoup.getfenghuangLotty(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "http://5559965.com网的" + code + " 时间超", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "http://5559965.com网的" + code + " 正在开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "http://5559965.com网的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            Assert.isTrue(!(lotteryData == null), "没有抓取到信息");
            // 1.2 抓取成功 next
            // 2.将版本升级

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("fenghuangwang"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("fenghuangwang");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "http://5559965.com网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "http://5559965.com网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void addlblottery(String code, String url) {
        LotteryData lotteryData = null;
        List<LotteryData> lotteryDatas = new ArrayList<>();
        try {
            // 1.去其他网站抓取信息
            //log.info("开始抓取" + "http://5559965.com网的" + code + "信息");
            int state = 0;
            while (state < 5) {
                try {
                    JsoupUtil jsoup = new JsoupUtil();
                    lotteryDatas = jsoup.getlebaLotty(url, code);
                } catch (SocketTimeoutException e) {
                    log.error("访问 " + "乐8网的" + code + " 时间超", e.getMessage());
                } catch (ParseException e) {
                    log.error("访问 " + "乐8网的" + code + " 正在开奖", e.getMessage());
                } catch (Exception e) {
                    log.error("访问 " + "乐8网的" + code + "其它异常信息", e.getMessage());
                }
                if (lotteryData == null) {
                    state++;
                } else {
                    break;
                }
            }
            int j = 0;
            for (LotteryData lotteryData1 : lotteryDatas) {
                lotteryData1.setCode(code);
                j++;
                if (j > 10) {
                    break;
                }
                redisTemplate.opsForSet().add("setLotteryData", lotteryData1);
            }
            lotteryData = lotteryDatas.get(0);
            Assert.isTrue(!(lotteryData == null), "没有抓取到信息");
            // 1.2 抓取成功 next
            // 2.将版本升级

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LotteryInformation lotteryInformation = null;
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(code, lotteryData.getExpect(), lotteryData.getOpencode());
            if (lotteryInformation != null) {
                if (lotteryInformation.getVersion() > 0) {
                    return;
                }
                if (lotteryInformation.getOpenCode().equals(lotteryData.getOpencode()) && !("l8"
                        .equals(lotteryInformation.getSource()))) {
                    lotteryInformationDao.addVersion(lotteryInformation.getId());
                }
                return;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(code);
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            lotteryInformation.setState("NORMAL");
            if (judge(code)) {
                lotteryInformation.setVersion(2);
            } else {
                lotteryInformation.setVersion(0);
            }
            lotteryInformation.setSource("l8");
            lotteryInformationDao.save(lotteryInformation);
            log.info("[开奖信息]" + "[开奖彩种]:" + lotteryInformation.getLotteryCode() + " ,[开奖期号]:"
                    + lotteryInformation.getLotteryExpect() + " ,[开奖号码]:" + lotteryInformation.getOpenCode()
                    + " ,[开奖源]:" + lotteryInformation.getSource());
            //log.info("结束抓取" + "http://5559965.com网的" + code + "开奖信息");
        } catch (Exception e) {
            log.error("添加" + "乐8网的" + code + "开奖信息失败", e.getMessage());
        }
    }

    @Override
    public void addcheck_data(Set<LotteryData> set) {
        LotteryInformation lotteryInformation = null;
        List<LotteryInformation> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (LotteryData lotteryData : set) {
            lotteryInformation = lotteryInformationDao
                    .findByCodeAndExpect(lotteryData.getCode(), lotteryData.getExpect(),
                            lotteryData.getOpencode());
            if (lotteryInformation != null) {
                continue;
            }
            lotteryInformation = new LotteryInformation();
            lotteryInformation.setLotteryCode(lotteryData.getCode());
            lotteryInformation.setLotteryExpect(lotteryData.getExpect());
            lotteryInformation.setOpenCode(lotteryData.getOpencode());
            try {
                lotteryInformation.setOpenTime(sdf.parse(lotteryData.getOpentime()));
            } catch (ParseException e) {
                log.error("添加" + "乐8网的补号" + lotteryData.getCode() + "失败", e.getMessage());
            }
            lotteryInformation.setState("NORMAL");
            lotteryInformation.setVersion(6);
            lotteryInformation.setSource("l8");
            list.add(lotteryInformation);
        }
        if (list.size() > 0) {
            int a = lotteryInformationDao.insertAll(list);
            redisTemplate.delete("setLotteryData");

        }
    }

    /**
     * 时间字符串互相转化
     */
    public Date stringToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date = sdf.parse(time);
        return date;
    }

    public Date DateToStringToDate(Date time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time1 = sdf.format(time);
        time1 = time1 + " 09:30:00";
        return stringToDate(time1);
    }

    public String StringToString(String time) throws ParseException {
        time = time.replaceAll("-", "");
        return time.substring(0, 8);
    }

    /***
     * 判断是否需要保存
     * HF_JLK3,HF_GDKL10F,HF_SHD11,HF_AHK3,HF_GXK3,HF_JSK3,,HF_BJK3,HF_BJ28,HF_XJSSC,HF_GDD11,HF_SHSSL
     * @return
     */
    public boolean judge(String code) {
        boolean flag = false;
        switch (code) {
            case "HF_JLK3":
           /* case "HF_GDKL10F":
            case "HF_SHD11":
            case "HF_AHK3":
            case "HF_SHSSL":
            case "HF_GXK3":
            case "HF_JSK3":*/
            case "HF_BJK3":
            case "HF_AHD11":
                //case "HF_BJ28":
            /*case "HF_PL3":
            case "HF_FC3D":
            case "MARK_SIX":*/
           /* case "HF_XJSSC":
            case "HF_GDD11":
            case "HF_XYFT":

            case "HF_JXD11":*/
                flag = true;
        }
        return flag;
    }
}

